package com.fate.securecheckplatform;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.fate.securecheckplatform.base.BaseActivity;
import com.fate.securecheckplatform.constant.Constant;
import com.fate.securecheckplatform.ui.frags.ObtainFragment;
import com.fate.securecheckplatform.ui.frags.RegisterFragment;
import com.fate.securecheckplatform.ui.frags.SetFragment;
import com.fate.securecheckplatform.utils.SpUtil;
import com.fate.securecheckplatform.utils.ToastUtil;
import com.luck.picture.lib.tools.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    @BindView(R.id.tv_obtain)
    TextView mTvObtain;
    @BindView(R.id.tv_set)
    TextView mTvSet;
    @BindView(R.id.tv_login_name)
    TextView mTvLoginName;


    RegisterFragment registerFragment;
    ObtainFragment obtainFragment;
    SetFragment setFragment;
    FragmentTransaction transaction;
    FragmentManager fragmentManager;
    private int currentItemPosition = 0;
    private long currentTime = 0;
    private long exitTime = 0;
    private AlertDialog alertDialog;

    @Override
    protected void initView() {

        String loginName = SpUtil.getInstance().getString(Constant.LOGIN_NAME);
        mTvLoginName.setText(TextUtils.isEmpty(loginName) ? "" : loginName);
        fragmentManager = getSupportFragmentManager();

        changeLeftMenu(currentItemPosition);
        changeFragment();

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            exitApp();
        }
        return true;
    }

    private void exitApp() {
        if ((System.currentTimeMillis() - exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
        {
            ToastUtil.toast("再点一次退出");
            exitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }

    }


    @OnClick({R.id.tv_register, R.id.tv_obtain, R.id.tv_set, R.id.iv_shut_down})
    public void onClickEvent(View v) {
        switch (v.getId()) {
            case R.id.tv_register:
                currentItemPosition = 0;
                changeLeftMenu(0);
                changeFragment();
                break;
            case R.id.tv_obtain:
                currentItemPosition = 1;
                changeLeftMenu(1);
                changeFragment();
                break;
            case R.id.tv_set:
                currentItemPosition = 2;
                changeLeftMenu(2);
                changeFragment();
                break;
            case R.id.iv_shut_down:
                showExistDialog();
                break;
        }
    }

    private void showExistDialog() {
        alertDialog = new AlertDialog.Builder(MainActivity.this)
                .setTitle("退出程序")
                .setMessage("是否退出程序")
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    SpUtil.getInstance().putString(Constant.LOGIN_NAME, "");
                    SecureCheckApplication.get().exit();
                })
                .setNegativeButton("取消", (dialogInterface, i) -> {
                    alertDialog.dismiss();
                }).create();
        alertDialog.show();
    }

    /**
     * @param i 选中的位置
     */
    private void changeLeftMenu(int i) {
        switch (i) {
            case 0:
                mTvRegister.setSelected(true);
                mTvObtain.setSelected(false);
                mTvSet.setSelected(false);
                break;
            case 1:
                mTvRegister.setSelected(false);
                mTvObtain.setSelected(true);
                mTvSet.setSelected(false);
                break;
            case 2:
                mTvRegister.setSelected(false);
                mTvObtain.setSelected(false);
                mTvSet.setSelected(true);
                break;
        }
    }

    /**
     * 切换Fragment
     */
    private void changeFragment() {

        transaction = fragmentManager.beginTransaction();

        hideFragments(transaction);
        switch (currentItemPosition) {
            case 0:
                if (registerFragment == null) {
                    registerFragment = RegisterFragment.get();
                    transaction.add(R.id.fl_content, registerFragment);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(registerFragment);
                }
                break;
            case 1:
                if (obtainFragment == null) {
                    obtainFragment = ObtainFragment.get();
                    transaction.add(R.id.fl_content, obtainFragment);
                } else {
                    transaction.show(obtainFragment);
                }
                break;
            case 2:
                if (setFragment == null) {
                    setFragment = SetFragment.get();
                    transaction.add(R.id.fl_content, setFragment);
                } else {
                    transaction.show(setFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (registerFragment != null) {
            transaction.hide(registerFragment);
        }
        if (obtainFragment != null) {
            transaction.hide(obtainFragment);
        }
        if (setFragment != null) {
            transaction.hide(setFragment);
        }
    }

}

